#include "sm_uart.h"
#include "esp_err.h"





void sm_uart_init(sm_uart_obj *obj)
{
    /* Configure UART. Note that REF_TICK is used so that the baud rate remains
     * correct while APB frequency is changing in light sleep mode.
     */
    const uart_config_t uart_config = 
    {
	    .baud_rate = obj->baud,
	    .data_bits = UART_DATA_8_BITS,
	    .parity = UART_PARITY_DISABLE,
	    .stop_bits = UART_STOP_BITS_1,
    #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
	    .source_clk = UART_SCLK_REF_TICK,
    #else
	    .source_clk = UART_SCLK_XTAL,
    #endif
    };

    /* Install UART driver for interrupt-driven reads and writes */
    ESP_ERROR_CHECK( uart_driver_install(obj->uart_num,256, 0, 0, NULL, 0));

    ESP_ERROR_CHECK( uart_param_config(obj->uart_num, &uart_config));
//     if(obj->tx_io_num != -1 && obj->rx_io_num != -1 && obj->rts_io_num != -1 && obj->cts_io_num)
//     {
	ESP_ERROR_CHECK( uart_set_pin(obj->uart_num,obj->tx_io_num,obj->rx_io_num,obj->rts_io_num,obj->cts_io_num));
//     }
}





